package com.tssup.fesco.service.impl;

import com.tssup.fesco.common.constants.OfcConstants;
import com.tssup.fesco.common.constants.ReturnCodeConstants;
import com.tssup.fesco.common.exeception.RetException;
import com.tssup.fesco.common.vo.Context;
import com.tssup.fesco.common.vo.OperatorVo;
import com.tssup.fesco.sales.ofc.persistent.mapper.OfcIOrderInfoMapper;
import com.tssup.fesco.sales.ofc.persistent.mapper.OfcLOrderTrackinginfoMapper;
import com.tssup.fesco.sales.ofc.persistent.mapper.OfcLSystemlogMapper;
import com.tssup.fesco.sales.ofc.persistent.pojo.OfcIOrderInfo;
import com.tssup.fesco.sales.ofc.persistent.pojo.OfcLOrderTrackinginfo;
import com.tssup.fesco.sales.ofc.persistent.pojo.OfcLSystemlogWithBLOBs;
import com.tssup.fesco.service.OfcOrderLogService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 *
 * @author haomin
 * @date 2017/3/31
 */
@Service
public class OfcOrderLogServiceImpl implements OfcOrderLogService {

    @Autowired
    private OfcIOrderInfoMapper orderInfoMapper;

    @Autowired
    private OfcLOrderTrackinginfoMapper orderTrackinginfoMapper;

    @Autowired
    private OfcLSystemlogMapper systemlogMapper;

    /**
     * 保存订单状态跟踪信息
     * @param orderId 订单编码
     * @param orderStatus 订单状态
     * @param remark 备注
     * @throws Exception
     */
    @Override
    @Transactional
    public void saveOrderTraceingInfo(String orderId, Integer orderStatus, String operator,  String remark) throws Exception{
        Date nowDate = new Date();
        OfcLOrderTrackinginfo orderTrackinginfo = new OfcLOrderTrackinginfo();
        orderTrackinginfo.setOrderId(orderId);
        orderTrackinginfo.setOrderStatus(orderStatus);
        orderTrackinginfo.setOperator(operator);
        orderTrackinginfo.setRemark(remark);
        orderTrackinginfo.setCreatedAt(nowDate);
        orderTrackinginfo.setOperationTime(nowDate);
        orderTrackinginfoMapper.insert(orderTrackinginfo);
    }

    /**
     *
     * @param orderIds
     * @param orderStatus
     * @param operator
     * @param remark
     * @throws Exception
     */
    @Override
    @Transactional
    public void batchSaveOrderTraceingInfo(List<String> orderIds, Integer orderStatus, String operator, String remark) throws Exception {
        List<OfcLOrderTrackinginfo> orderTrackinginfos = new ArrayList<>();
        Date nowDate = new Date();
        orderIds.forEach(orderId ->{
            OfcLOrderTrackinginfo orderTrackinginfo = new OfcLOrderTrackinginfo();
            orderTrackinginfo.setOrderId(orderId);
            orderTrackinginfo.setOrderStatus(orderStatus);
            orderTrackinginfo.setOperator(operator);
            orderTrackinginfo.setRemark(remark);
            orderTrackinginfo.setCreatedAt(nowDate);
            orderTrackinginfo.setOperationTime(nowDate);
            orderTrackinginfos.add(orderTrackinginfo);
        });
        orderTrackinginfoMapper.batchInsert(orderTrackinginfos);
    }

    /**
     * 保存订单改动到日志表
     * @param orderInfo
     * @param content
     * @param remark
     * @param isManual
     */
    @Override
    @Transactional
    public void saveSysLog(OfcIOrderInfo orderInfo, String orderId, String content, String remark, boolean isManual){
        if(null == orderInfo){
            orderInfo = Optional.ofNullable(orderInfoMapper.selectByOrderId(orderId))
                    .orElseThrow(() -> new RetException(ReturnCodeConstants.INVALID_PARAMETER_CODE, OfcConstants.ORDER_PROBLEM.PROBLEM_MESSAGE.ORDER_INFO_MISS));
        }

        OfcLSystemlogWithBLOBs systemlog = new OfcLSystemlogWithBLOBs();
        systemlog.setOrderId(orderInfo.getOrderId());
        systemlog.setOrderStatus(orderInfo.getOrderStatus());
        systemlog.setOrderTags(orderInfo.getOrderFlags());
        systemlog.setSysLevel(OfcConstants.SYSTEM_LEVEL.OFC);
        if(isManual){
            //获取当前人工操作员
            OperatorVo operator = Context.getOperator();
            systemlog.setOperatorId(operator != null && !StringUtils.isEmpty(operator.getOperator()) ? operator.getOperator().toString() : "");
            systemlog.setOperatorName(operator != null && !StringUtils.isEmpty(operator.getOperaterName()) ? operator.getOperaterName() : "");
        }else{
            //填写为系统操作员
            systemlog.setOperatorId(OfcConstants.SYSTEM_OPERATOR.ID);
            systemlog.setOperatorName(OfcConstants.SYSTEM_OPERATOR.NAME);
        }
        systemlog.setContent(content);
        systemlog.setRemark(remark);
        systemlog.setCreatedAt(new Date());

        systemlogMapper.insert(systemlog);
    }

    /**
     *
     * @param orderInfos
     * @param orderIds
     * @param content
     * @param remark
     * @param isManual
     * @throws Exception
     */
    @Override
    @Transactional
    public void batchSaveSysLog(List<OfcIOrderInfo> orderInfos, List<String> orderIds, String content, String remark, boolean isManual) throws Exception {
        if ( CollectionUtils.isEmpty(orderInfos) && CollectionUtils.isEmpty(orderIds) ) {
            throw new RetException(ReturnCodeConstants.INVALID_PARAMETER_CODE, "订单信息不能为空");
        }
        if ( CollectionUtils.isEmpty(orderInfos) ) {
            orderInfos = Optional.ofNullable(orderInfoMapper.selectByOrderIds(orderIds))
                    .orElseThrow(() -> new RetException(ReturnCodeConstants.INVALID_PARAMETER_CODE, OfcConstants.ORDER_PROBLEM.PROBLEM_MESSAGE.ORDER_INFO_MISS));
        }

        //获取当前人工操作员
        OperatorVo operator = null;
        if ( isManual ) {
            operator = Context.getOperator();
        }
        List<OfcLSystemlogWithBLOBs> list = new ArrayList<OfcLSystemlogWithBLOBs>();
        OperatorVo finalOperator = operator;
        orderInfos.forEach(orderInfo -> {
            OfcLSystemlogWithBLOBs systemlog = new OfcLSystemlogWithBLOBs();
            systemlog.setOrderId(orderInfo.getOrderId());
            systemlog.setOrderStatus(orderInfo.getOrderStatus());
            systemlog.setOrderTags(orderInfo.getOrderFlags());
            systemlog.setSysLevel(OfcConstants.SYSTEM_LEVEL.OFC);
            //isManual为true，获取当前人工操作员，否则为系统操作员
            systemlog.setOperatorId(isManual ? finalOperator.getOperator().toString() : OfcConstants.SYSTEM_OPERATOR.ID);
            systemlog.setOperatorName(isManual ? finalOperator.getOperaterName() : OfcConstants.SYSTEM_OPERATOR.NAME);
            systemlog.setContent(content);
            systemlog.setRemark(remark);
            systemlog.setCreatedAt(new Date());
            list.add(systemlog);
        });
        systemlogMapper.batchInsert(list);
    }

}
